package com.boyadegroot.divelog.ui.diver.tabs;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;

import com.boyadegroot.divelog.R;
import com.boyadegroot.divelog.data.DiveLogDatabaseContract;
import com.boyadegroot.divelog.data.DiveLogProvider;
import com.boyadegroot.divelog.models.Diver;

public class DiverCerts extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final String TAG = DiverCerts.class.getName();

    private Diver mDiver;
    private DiverCertsAdapter mAdapter;

    public static DiverCerts newInstance(Diver diver) {
        // Log.v(TAG, "DiverCerts newInstance(diver = " + diver.getUsername() + ")");
        DiverCerts instance = new DiverCerts();
        Bundle args = new Bundle();
        args.putParcelable("diver", diver);
        instance.setArguments(args);
        return instance;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // Log.v(TAG, "onCreate(Bundle savedInstanceState)");
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            mDiver = getArguments().getParcelable("diver");
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Context context = getActivity();
        mAdapter = new DiverCertsAdapter(context, R.layout.listitem_certification);
        setListAdapter(mAdapter);
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        // Log.v(TAG, "onLoadFinished(Loader<Cursor> loader, Cursor data)");
        mAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        // Log.v(TAG, "onLoaderReset(Loader<Cursor> loader)");
        mAdapter.swapCursor(null);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // Log.v(TAG, "Loader<Cursor> onCreateLoader(int id, Bundle args)");
        Context context = getActivity();
        String diverId = Long.toString(mDiver.getId());
        Uri certsUri = Uri.withAppendedPath(DiveLogProvider.CONTENT_URI, DiveLogProvider.PATH_CERTS);
        CursorLoader cursorLoader = new CursorLoader(
                context,
                certsUri,
                new String[]{ // columns to request in the query
                        DiveLogDatabaseContract.DiveCertifications._ID,
                        DiveLogDatabaseContract.DiveCertifications.COLUMN_NAME_DATE,
                        DiveLogDatabaseContract.DiveCertifications.COLUMN_NAME_NUMBER,
                        DiveLogDatabaseContract.DiveCertifications.COLUMN_NAME_COURSE_ID
                },
                DiveLogDatabaseContract.DiveCertifications.COLUMN_NAME_DIVER_ID + " = ?",
                new String[]{diverId},
                DiveLogDatabaseContract.DiveCertifications.COLUMN_NAME_DATE);
        // Log.v(TAG, "Creating loader for diver with id = " + diverId);
        return cursorLoader;
    }
}